package com.ruoyi.listener.utils;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import com.taobao.api.ApiException;
import org.springframework.stereotype.Component;


@Component
public class DingDingUtils {
    public  void send(String url, String msg) {
        try {
            //创建钉钉客户端
            DingTalkClient client = new DefaultDingTalkClient(url);
            //构建自定义机器人请求
            OapiRobotSendRequest request = new OapiRobotSendRequest();
            //设置固定为文字版信息类型
            request.setMsgtype("text");
            //构建自定义机器人文字类型请求
            OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
            //调用封装文本信息。
            text.setContent(msg);
            request.setText(text);
            //构建自定义机器人@人范围请求
            OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
            //明确为 true。通知时@所有人
            at.setIsAtAll(true);
            request.setAt(at);
            OapiRobotSendResponse response = client.execute(request);
            System.out.println("发送完成，返回值：{"+response.getBody()+"}");
        } catch (ApiException e) {
            System.out.println("钉钉接口调用异常："+e);
        }
    }

}
